class Graph{
    constructor(isdirected = false){
        this.isdirected = isdirected;
        this.vertices = [];//存放顶点
        this.edge = new Map()//邻接点
    }
    addVertices(v){
        if(!this.vertices.includes(v)){
            this.vertices.push(v)
            this.edge.set(v,[])
        }
    }
    //边
    addEdge(v,w){
        if(!this.vertices.includes(v)){
            this.addVertices(v)
        }
        if(!this.vertices.includes(w)){
            this.addVertices(w)
        }
        this.edge.get(v).push(w)
        if(!this.isdirected){
            this.edge.get(w).push(v)
        }
    }
    //获取顶点
    getVertices(){
        return this.vertices
    }
    //获取邻接表
    getEdge(){
        return this.edge
    }
    //转字符串
    toString(){
        let str = '';
        for(let i = 0 ; i < this.vertices.length ; i++){
            str += `${this.vertices[i]}=>>>>`;
            let edgeList = this.edge.get(this.vertices[i]);
            for(let j = 0 ; j < edgeList.length ; j++){
                str += `${edgeList[j]}`
            }
            str += '\n'
        }
        return str
    }
}
let g = new Graph()
let arr = ['A','B','C','D','E','F','G']

for(let i = 0 ; i <arr.length ; i++){
    g.addVertices(arr[i])
}
g.addEdge('A','B');

g.addEdge('A','C');

g.addEdge('A','D');

g.addEdge('C','D');

g.addEdge('C','G');

g.addEdge('D','G');

g.addEdge('D','H');

g.addEdge('B','E');

g.addEdge('B','F');

g.addEdge('E','I');
console.log(g.getVertices());

console.log(g.getEdge());

console.log(g.toString());
console.log(g)